<template>
    <div>
        <p>
            <button class="btn btn-white btn-default btn-round" @click="refresh()">
                <i class="ace-icon fa fa-refresh red"></i>
                刷新
            </button>
            &nbsp;
            <button class="btn btn-white btn-success btn-round" @click="openAddModal()">
                <i class="ace-icon fa fa-edit blue"></i>
                新增
            </button>
        </p>
        <div class="row">
            <div class="col-md-8">


                <table id="simple-table" class="table  table-bordered table-hover">
                    <thead>
                    <tr>
<!--                        <th class="center">ID</th>-->
                        <th class="center">名称</th>
                        <th class="center">页面</th>
                        <th class="center">请求</th>
<!--                        <th class="center">父id</th>-->
                        <th class="center">操作</th>
                    </tr>
                    </thead>

                    <tbody>
                    <tr v-for="resource in resources">
<!--                        <td class="center">{{resource.id}}</td>-->
                        <td class="center" style="width: 120px">{{resource.name}}</td>
                        <td class="center">{{resource.page}}</td>
                        <td class="center">{{resource.request}}</td>
<!--                        <td class="center">{{resource.parent}}</td>-->
                        <td class="center" style="width:100px;">
                            <div class=" btn-group">
                                <button class="btn btn-white btn-xs btn-default btn-round"
                                        @click="openEditModal(resource.id)">
                                    <i class="ace-icon fa fa-edit bigger-120"></i>
                                </button>

                                <button class="btn btn-white btn-xs btn-danger btn-round"
                                        @click="deleteById(resource.id)">
                                    <i class="ace-icon fa fa-trash-o bigger-120"></i>
                                </button>
                            </div>

                        </td>
                    </tr>
                    <tr v-if="resources.length === 0">
                        <td colspan="6" class="center"
                            style="background-color: lightgrey;font-family: 'Courier New',serif">暂无数据
                        </td>
                    </tr>
                    </tbody>
                </table>
                <!-- 分页组件 -->
                <pagination ref="pagination" v-bind:list="list"></pagination>
            </div>
            <div class="col-md-4">
                <h4 class="modal-title bolder red"
                >资源管理</h4>
                <ul id="tree" class="ztree"></ul>
            </div>
        </div>

        <!-- 模态框 -->
        <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div style="width: 40%" class="modal-dialog" role="document">
                <div class="modal-content" style="border-color: #aa160e;background-color: lightblue">
                    <div class="modal-header">
                        <button type="button" class="close blue" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title grey bolder" style="font-family: 'Courier New', Courier, monospace"
                            id="myModalLabel">{{modalTitle}}</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"> 名称</label>
                                <div class="col-sm-9">
                                    <input v-model="resource.name" type="text" placeholder="名称|菜单或按钮"
                                           class="col-xs-10 col-sm-5"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"> 页面</label>
                                <div class="col-sm-9">
                                    <input v-model="resource.page" type="text" placeholder="页面|路由"
                                           class="col-xs-10 col-sm-5"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"> 请求</label>
                                <div class="col-sm-9">
                                    <input v-model="resource.request" type="text" placeholder="请求|接口"
                                           class="col-xs-10 col-sm-5"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label no-padding-right"> 父节点</label>
                                <div class="col-sm-4">
                                    <select v-model="resource.parent" class="form-control">
                                        <option  v-for="r in list_resources" v-bind:value="r.id">{{r.name}}</option>
                                    </select>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default btn-round"
                                style="font-family: 'Courier New', Courier, monospace" data-dismiss="modal">
                            关闭
                        </button>
                        <button @click="save()" type="button" class="btn btn-primary btn-round"
                                style="font-family: 'Courier New', Courier, monospace">
                            保存
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

</template>

<script>
    import Pagination from "../../components/pagination.vue"

    export default {
        name: "resource",
        components: {
            Pagination
        },
        data: function () {
            return {
                resources: [],
                tree_resources: [],
                list_resources: [],
                resource: {},
                modalTitleArr: {
                    insert: "新增资源",
                    update: "修改资源"
                },
                modalTitle: "",
                tree: {},

            }
        },
        mounted: function () {
            // this.$parent.activeSidebar("business-resource-sidebar")
            let _this = this;
            _this.list();
            _this.getTreeResources();
        },
        methods: {
            /*获取所有的资源【列表显示】*/
            getAllResourcesList() {
                let _this = this;
                _this.$ajax.get(process.env.VUE_APP_SERVER + '/system/admin/resource/all').then((response) => {
                    _this.list_resources = response.data.data;
                    let r = {id:"0",name:"根目录"};
                    _this.list_resources.push(r);
                });
            },

            /*获取树形资源的数据*/
            getTreeResources() {
                let _this = this;
                _this.$ajax.get(process.env.VUE_APP_SERVER + '/system/admin/resource/findTreeResources').then((response) => {
                    _this.tree_resources = response.data.data;
                    _this.initTree();
                });
            },
            /**
             * 初始资源树
             */
            initTree() {
                let _this = this;
                let setting = {
                    data: {
                        simpleData: {
                            idKey: "id",
                            pIdKey: "parent",
                            rootPId: "",
                            // enable: true
                        }
                    }
                };
                _this.zTree = $.fn.zTree.init($("#tree"), setting, _this.tree_resources);
                _this.zTree.expandAll(true);
            },

            /* 打开新增模态框 */
            openAddModal() {
                let _this = this;
                _this.clearResource();
                _this.modalTitle = _this.modalTitleArr.insert;
                _this.getAllResourcesList();
                $("#myModal1").modal({backdrop: 'static'});
                $("#myModal1").modal("show");

            },
            /*打开编辑模态框*/
            openEditModal(id) {
                let _this = this;
                _this.clearResource();
                _this.modalTitle = _this.modalTitleArr.update;
                _this.$ajax.get(process.env.VUE_APP_SERVER + '/system/admin/resource/findById/' + id).then((response) => {
                    _this.resource = response.data.data;
                });
                _this.getAllResourcesList();
                $("#myModal1").modal({backdrop: 'static'});
                $("#myModal1").modal("show");
            },
            refresh() {
                let _this = this;
                Loading.show();
                _this.list(_this.$refs.pagination.page);
                Loading.hide();
            },
            /* 查找资源管理 */
            list(page = 1) {
                let _this = this;
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/system/admin/resource/list', {
                    page: page,
                    size: _this.$refs.pagination.size
                }).then((response) => {
                    _this.resources = response.data.data.list;
                    _this.$refs.pagination.render(page, response.data.data.total);
                })
            },
            /*保存资源管理信息到后台*/
            save() {
                let _this = this;
                if (!_this.resource.id) {
                    /*不存在id为新增*/
                    _this.insert();
                } else {
                    /*存在id为修改*/
                    _this.update();
                }
            },
            /*新增资源管理*/
            insert() {
                let _this = this;
                /*新增校验*/
                /*if (!Validator.require(_this.resource.name,"名称")
                || !Validator.require(_this.resource.courseId, "课程")
                || !Validator.length(_this.resource.courseId,"课程ID",1,19)) {
                    return;
                }*/
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/system/admin/resource/save', _this.resource).then((response) => {
                    console.log(response);
                    if (response.data.success) {
                        _this.list(_this.$refs.pagination.page);
                        Toast.success(response.data.message);
                        _this.getTreeResources();
                    } else {
                        Toast.error(response.data.message);
                    }
                    $("#myModal1").modal("hide");
                });
            },
            /*修改资源管理*/
            update() {
                let _this = this;
                /*更新校验*/
                /*if (!Validator.require(_this.resource.name,"名称")
                        && !Validator.require(_this.resource.courseId, "课程")
                        || !Validator.length(_this.resource.courseId,"课程ID",1,19)) {
                    return;
                }*/
                _this.$ajax.put(process.env.VUE_APP_SERVER + '/system/admin/resource/update', _this.resource).then((response) => {
                    if (response.data.success) {
                        _this.list(_this.$refs.pagination.page);
                        Toast.success(response.data.message);
                        _this.getTreeResources();
                    } else {
                        Toast.error(response.data.message);
                    }
                    $("#myModal1").modal("hide");
                })
            },
            /*删除资源管理*/
            deleteById(id) {
                let _this = this;
                /*删除提示框*/
                Confirm.show("", () => {
                    _this.$ajax.delete(process.env.VUE_APP_SERVER + '/system/admin/resource/deleteById/' + id).then((response) => {
                        if (response.data.success) {
                            _this.list(_this.$refs.pagination.page);
                            Toast.success(response.data.message);
                            _this.getTreeResources();
                        } else {
                            Toast.error(response.data.message);
                        }
                        $("#myModal1").modal("hide");
                    })
                });
            },
            /*清空模态框的数据*/
            clearResource() {
                let _this = this;
                _this.resource = {};
                _this.modalTitle = "";
            }
        }
    }
</script>
